package cn.tedu.egnotice0707.controller;

import cn.tedu.egnotice0707.base.response.JsonResult;
import cn.tedu.egnotice0707.base.response.StatusCode;
import cn.tedu.egnotice0707.mapper.NoticeMapper;
import cn.tedu.egnotice0707.pojo.dto.NoticeAddParam;
import cn.tedu.egnotice0707.pojo.dto.NoticeAddQuery;
import cn.tedu.egnotice0707.pojo.entity.Notice;
import cn.tedu.egnotice0707.pojo.vo.NoticeDetailInfoVO;
import cn.tedu.egnotice0707.pojo.vo.NoticeListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@Slf4j
@RequestMapping("/v1/notice/")
@RestController
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;
    @RequestMapping("add")
    public JsonResult add(@RequestBody NoticeAddParam noticeAddParam){
        log.debug("noticeAddParam="+noticeAddParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam, notice);
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        log.debug("notice="+notice);
        noticeMapper.insertNotice(notice);
        return  JsonResult.ok();//没有数据返回的
    }
    @RequestMapping("list")

    public JsonResult list(@RequestBody NoticeAddQuery noticeAddQuery){
        log.debug("noticeAddQuery="+noticeAddQuery);
        List<NoticeListVO> noticeListVOS=noticeMapper.list(noticeAddQuery);
        return JsonResult.ok(noticeListVOS);
    }
    @RequestMapping("detail")
    public JsonResult detail(Long id){
        log.debug("id="+id);
        NoticeDetailInfoVO noticeDetailInfoVO=noticeMapper.detail(id);
        return JsonResult.ok(noticeDetailInfoVO);
    }
    @RequestMapping("del")
    public JsonResult del(Long id){
        log.debug("id="+id);
        int i=noticeMapper.deleteNoticeById(id);
        if(i>0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }
    @RequestMapping("update")
    public JsonResult update(@RequestBody NoticeAddParam noticeAddParam){
        log.debug("noticeAddParam="+noticeAddParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam, notice);
        notice.setUpdateTime(new Date());
        log.debug("notice="+notice);
        int i=noticeMapper.updateNotice(notice);
        if(i>0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }



}
